<?php

/**
 * Principal property
 *
 * The principal property represents a principal from RFC3744 (ACL).
 * The property can be used to specify a principal or pseudo principals. 
 *
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_ACL_Property_HrefPrincipal 
	extends Slash_ACL_Property_Principal
{

    /**
     * Url to principal
     *
     * This value is only used for the HREF principal type.
     * 
     * @var Slash_ACL_Property_Href
     */
    private $href;

    /**
     * Creates the property.
     *
     * @param int $type 
     * @param mixed $href 
     * @return void
     */
    public function __construct($href) 
    {
    	//parent::__construct(self::HREF);
      if ($href instanceof Slash_ACL_Property_Href)
      {
      	$this->href = $href;
      } else {
      	$this->href = new Slash_ACL_Property_Href($href);
      }
    }


	/*
	 * Return true if this (property) principal includes the property Principal 
	 * $princ.
	 * This method is called by the Acl property's grantsPrivilege() method, 
	 * and the principal it passes to this method is made from the current user, or
	 *  else is UNAUTHENTICATED.
	 */  
	public function includesPrincipal(Slash_ACL_Property_Principal $princ, Sabre_DAV_FSExt_Node $resourceNode)
	{
		/*
		 * An Href principal includes another principal if it is the same principal, 
		 * or if the latter is a member of the former.
		 */
		$myHref = $this->getHref();
		Slash_Debug::debug("ACL_Property_HrefPrincipal includesPrincipal: $myHref->". $princ->toString());
		$hisHref = $princ->getHref();
		if ($myHref == $hisHref)
		{
			return true;
		} 
		$plugin = Slash_ACL_Plugin::getInstance();
		$thisnode = $plugin->server->tree->getNodeForPath($myHref);
		$propName = "{DAV:}group-member-set";
		$propreq = array($propName);
		$props = $thisnode->getProperties($propreq);
		if (isset($props[$propName]))
		{
			$gms = $props[$propName];
			Slash_Debug::debug("ACL_Property_HrefPrincipal includesPrincipal: got my group-member-set");
			$hrefs = $gms->getHrefs();
			foreach($hrefs as $href)
			{
				Slash_Debug::debug("ACL_Property_HrefPrincipal includesPrincipal: group-member ". $href->getHref());
				if ($href->getHref() == $hisHref)
				{
					Slash_Debug::debug("ACL_Property_HrefPrincipal includesPrincipal: matched group member set");
					return true;
				}
			}
		}
		return false;
	}
    /**
     * Returns the principal uri. 
     * 
     * @return string
     */
    public function getHref() 
    {
        return $this->href->getHref();
    }

  public function equals(Slash_ACL_Property_Principal $princ)
  {
  	$result = false;
 		if ($this->getHref() == $princ->getHref())
 		{
 			$result = true;
 		}
  	return $result;
  }
  
  public function toString()
  {
  	return "href:" . $this->href->getHref();
  }
  
  public static function fromString($princStr)
	{
		//$extra is a clarked property name and a prefix
		$parts = explode(":", $princStr, 2);
		return new self(new Slash_ACL_Property_Href($parts[1]));
	}
  
  static function unserialize(DOMElement $princElement) 
	{
		$hrefElement = $princElement->getElementsByTagNameNS("urn:DAV", "href")->item(0);		
   	return new self(Slash_ACL_Property_Href::unserialize($hrefElement));
	}

	public function serialize(Sabre_DAV_Server $server, DOMElement $princElement) 
  {
    $prefix = $server->xmlNamespaces['DAV:'];
    $doc = $princElement->ownerDocument;
   	$href = $doc->createElement("$prefix:href");
   	$this->href->serialize($server, $href);
    $princElement->appendChild($href);
  }
}
